﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Domain.Values
{
	using System;
	using System.Web.Mvc;
	using Falcon.Core;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;
	using Falcon.Forums.Domain.Enums;
	using Falcon.Forums.Domain.Services;

	public class ForumUpdate : ValueBase<ForumUpdate>
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ForumUpdate"/> class.
		/// </summary>
		public ForumUpdate()
		{
			this.ForumService = Application.Resolve<IForumService>();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ForumUpdate"/> class.
		/// </summary>
		/// <param name="forum">The forum.</param>
		public ForumUpdate(Forum forum) : this()
		{
			this.Id = forum.Id;
			this.ParentId = forum.ParentId;
			this.Title = forum.Title;
			this.Body = forum.Body;
			this.Password = forum.Password;
			this.Html = forum.Html;
			this.BBCode = forum.BBCode;
			this.Smilies = forum.Smilies;
			this.Scope = forum.Scope;
			this.Status = forum.Status;
			this.ViewAge = forum.ViewAge;
		}

		/// <summary>
		/// Gets or sets the forum repository.
		/// </summary>
		/// <value>The forum repository.</value>
		public IForumService ForumService
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets the id.
		/// </summary>
		/// <value>The id.</value>
		public int Id
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the parent id.
		/// </summary>
		/// <value>The parent id.</value>
		public int ParentId
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public string Title
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the body.
		/// </summary>
		/// <value>The body.</value>
		public string Body
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the password.
		/// </summary>
		/// <value>The password.</value>
		public string Password
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this <see cref="ForumUpdate"/> is HTML.
		/// </summary>
		/// <value><c>true</c> if HTML; otherwise, <c>false</c>.</value>
		public bool Html
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether [BB code].
		/// </summary>
		/// <value><c>true</c> if [BB code]; otherwise, <c>false</c>.</value>
		public bool BBCode
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this <see cref="ForumUpdate"/> is smilies.
		/// </summary>
		/// <value><c>true</c> if smilies; otherwise, <c>false</c>.</value>
		public bool Smilies
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the scope.
		/// </summary>
		/// <value>The scope.</value>
		public ForumScope Scope
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the status.
		/// </summary>
		/// <value>The status.</value>
		public ForumStatus Status
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the view age.
		/// </summary>
		/// <value>The view age.</value>
		public ForumViewAge ViewAge
		{
			get;
			set;
		}

		/// <summary>
		/// Gets the parents.
		/// </summary>
		/// <value>The parents.</value>
		public SelectList Parents
		{
			get
			{
				var forums = ForumService.GetAll(new ForumSpecification
				{
					ExcludedId = this.Id,
					SkipPermissions = true
				});

				return new SelectList(forums, "Id", "Title", this.ParentId);
			}
		}

		/// <summary>
		/// Gets the scopes.
		/// </summary>
		/// <value>The scopes.</value>
		public SelectList Scopes
		{
			get
			{
				return new SelectList(Enum.GetNames(typeof(ForumScope)), this.Scope);
			}
		}

		/// <summary>
		/// Gets the statuses.
		/// </summary>
		/// <value>The statuses.</value>
		public SelectList Statuses
		{
			get
			{
				return new SelectList(Enum.GetNames(typeof(ForumStatus)), this.Status);
			}
		}

		/// <summary>
		/// Gets the view ages.
		/// </summary>
		/// <value>The view ages.</value>
		public SelectList ViewAges
		{
			get
			{
				return new SelectList(Enum.GetNames(typeof(ForumViewAge)), this.ViewAge);
			}
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<ForumUpdate> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Title)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Title));

			return rules;
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}